package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.Shop;
import com.ruoyi.system.service.IShopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CompanySupply;
import com.ruoyi.system.service.ICompanySupplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公司-供货商绑定关系Controller
 * 
 * @author ruoyi
 * @date 2025-08-01
 */
@RestController
@RequestMapping("/system/supply")
public class CompanySupplyController extends BaseController
{
    @Autowired
    private ICompanySupplyService companySupplyService;
    @Autowired
    private IShopService shopService;

    /**
     * 处理CompanySupply，判断自己的权限，加入限制条件
     * @param companySupply
     * @return
     */
    private CompanySupply filterCompany(CompanySupply companySupply){
         // 判断是否为超级管理员，是则不做限制
        if(!isAllCompany()){
            LoginUser loginUser = getLoginUser();
            companySupply.setCompanyId(loginUser.getCompany_id());
        }

        return companySupply;
    }

    /**
     * 重新返送短信
     * @return
     */
    @RequestMapping("/reSendMSG")
    public AjaxResult reSendMSG(@RequestParam("scid")long scid){
        AjaxResult result = companySupplyService.reSendMSG(scid);
        return result;
    }

    /**
     * 查询公司-供货商绑定关系列表
     */
    @PreAuthorize("@ss.hasRole('company')")
    @GetMapping("/list")
    public TableDataInfo list(CompanySupply companySupply)
    {
        startPage();
        companySupply=filterCompany(companySupply);
        List<CompanySupply> list = companySupplyService.selectCompanySupplyList(companySupply);
        return getDataTable(list);
    }

    /**
     * 导出公司-供货商绑定关系列表
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "公司-供货商绑定关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CompanySupply companySupply)
    {
        List<CompanySupply> list = companySupplyService.selectCompanySupplyList(companySupply);
        ExcelUtil<CompanySupply> util = new ExcelUtil<CompanySupply>(CompanySupply.class);
        util.exportExcel(response, list, "公司-供货商绑定关系数据");
    }

    /**
     * 获取公司-供货商绑定关系详细信息
     */
    @PreAuthorize("@ss.hasRole('company')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(companySupplyService.selectCompanySupplyById(id));
    }

    /**
     * 新增公司-供货商绑定关系
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "公司-供货商绑定关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanySupply companySupply)
    {
        return companySupplyService.insertCompanySupply(companySupply);
    }

    /**
     * 修改公司-供货商绑定关系
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "公司-供货商绑定关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanySupply companySupply)
    {
        return toAjax(companySupplyService.updateCompanySupply(companySupply));
    }

    /**
     * 删除公司-供货商绑定关系
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "公司-供货商绑定关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(companySupplyService.deleteCompanySupplyByIds(ids));
    }
}
